package com.gragra.sampling.nonproj.structures.tests;
import static org.junit.Assert.*;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import java.util.ArrayList;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.nonproj.ConvertHeadedFactory;
import com.gragra.sampling.nonproj.ConvertHeadedStructure;
import com.gragra.sampling.nonproj.FixedParsesAssigner;
import com.gragra.sampling.nonproj.Uniform;
import com.gragra.sampling.nonproj.generators.CreateDependencyCorpus;
import com.gragra.sampling.nonproj.generators.nonprojective.CreateGibbsNonprojective;
import com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree;
/**
 * 
 * @author Christoph Teichmann
 * 11.10.2013
 */
public class TestGibbsNonprojective
{
	/**
	 * 
	 */
	private ThreadedUniformAccess	tua;
	/**
	 * 
	 */
	private NonProjDepdTree	tree;
	@Test
	public void testResample1()
	{
		CreateDependencyCorpus cre = new CreateGibbsNonprojective(new ConvertHeadedFactory());
		tree = cre.makeTree("0|0 1|1 2|2 3|3", 5, 0.1);
		tua = new ThreadedUniformAccess(5);
		Uniform uni = new Uniform(5);
		tree.initialize(uni, tua);
		for(int i=0;i<100000;++i)
		{
			for(int thread=0;thread<5;++thread)
			{tree.resample(uni, 1.0, thread, tua);}
			tree.addSample();
		}
		Object2IntOpenHashMap<String> m = (((ConvertHeadedStructure) tree.getConverter()).getCounts());
		assertEquals(m.size(),125);
		double max = 0.0;
		for(String s : m.keySet())
		{
			double v1 = m.getInt(s);
			for(String t : m.keySet())
			{
				double v2 = m.getInt(t);
				max = Math.max(max, v1/v2);
			}
		}
		assertEquals(max,1.0,1.0);
	}
	@Test
	public void testResample2()
	{
		CreateDependencyCorpus cre = new CreateGibbsNonprojective(new ConvertHeadedFactory());
		tree = cre.makeTree("0|0 1|1 2|2 3|3", 5, 0.1);
		tua = new ThreadedUniformAccess(5);
		ArrayList<String> golds = new ArrayList<String>();
		golds.add("2 4 1 0");
		golds.add("4 0 1 2");
		FixedParsesAssigner fpa = new FixedParsesAssigner(5, 3.0, 2, golds);
		tree.initialize(fpa, tua);
		for(int i=0;i<100000;++i)
		{
			for(int thread=0;thread<5;++thread)
			{tree.resample(fpa, 1.0, thread, tua);}
			tree.addSample();
		}
		assertTrue(tree.getNumberSeen(golds.get(0))>0);
		double max = Math.max((double) tree.getNumberSeen(golds.get(0))/(double) tree.getNumberSeen(golds.get(1)), (double) tree.getNumberSeen(golds.get(1))/(double) tree.getNumberSeen(golds.get(0)));
		assertEquals(max,1.0,1.0);
	}
}